package com.tsinjie.metadata.database.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LiuXunming on 2015-10-13.
 * 数据库信息
 */
public class Database implements Serializable {
    /**
     * 数据库连接
     */
    private String url;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 数据库方言
     */
    private String dialect;
    /**
     * 是否只读
     */
    private boolean readonly;
    /**
     * 数据库名称
     */
    private String databaseProductName;
    /**
     * 数据库版本
     */
    private String databaseProductVersion;
    /**
     * 驱动名称
     */
    private String driverName;
    /**
     * 驱动版本
     */
    private String driverVersion;
    /**
     * schemas
     */
    private List<String> schemas;
    /**
     * 数据库表
     */
    private List<Table> tables;
    /**
     * 数据库表Map
     */
    private Map<String, Table> tableMap;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDialect() {
        return dialect;
    }

    public void setDialect(String dialect) {
        this.dialect = dialect;
    }

    public Map<String, Table> getTableMap() {
        return tableMap;
    }

    public void setTableMap(Map<String, Table> tableMap) {
        this.tableMap = tableMap;
    }

    public boolean isReadonly() {
        return readonly;
    }

    public void setReadonly(boolean readonly) {
        this.readonly = readonly;
    }

    public String getDatabaseProductName() {
        return databaseProductName;
    }

    public void setDatabaseProductName(String databaseProductName) {
        this.databaseProductName = databaseProductName;
    }

    public String getDatabaseProductVersion() {
        return databaseProductVersion;
    }

    public void setDatabaseProductVersion(String databaseProductVersion) {
        this.databaseProductVersion = databaseProductVersion;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getDriverVersion() {
        return driverVersion;
    }

    public void setDriverVersion(String driverVersion) {
        this.driverVersion = driverVersion;
    }

    public List<String> getSchemas() {
        return schemas;
    }

    public void setSchemas(List<String> schemas) {
        this.schemas = schemas;
    }

    public List<Table> getTables() {
        return tables;
    }

    public void setTables(List<Table> tables) {
        this.tables = tables;
    }

    /**
     * 根据表名称获取表对象
     *
     * @param tableName
     * @return
     */
    public Table getTable(String tableName) {
        if (tables == null) {
            return null;
        }
        if (tableMap == null) {
            tableMap = new HashMap<String, Table>();
            for (Table t : tables) {
                tableMap.put(t.getTableName(), t);
            }
        }
        return tableMap.get(tableName);
    }

    /**
     * 添加表
     *
     * @param table
     */
    public void addTable(Table table) {
        if (tables == null) {
            tables = new ArrayList<Table>();
        }
        tables.add(table);
    }

    @Override
    public String toString() {
        return "Database{" +
                "url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", readonly=" + readonly +
                ", databaseProductName='" + databaseProductName + '\'' +
                ", databaseProductVersion='" + databaseProductVersion + '\'' +
                ", driverName='" + driverName + '\'' +
                ", driverVersion='" + driverVersion + '\'' +
                ", schemas=" + schemas +
                ", tables=" + tables +
                '}';
    }
}
